﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Browser;

namespace MSNGame1
{
    /// <summary>
    /// Wrapper object for window.external.Users.
    /// </summary>
    public class Users : IEnumerable<Users.User>
    {
        ScriptObject usersObject; 
        
        public Users ()
	    {
            usersObject = (ScriptObject)((ScriptObject)HtmlPage.Window.GetProperty("external")).GetProperty("Users");
	    }
        
        /// <summary>
        /// Amount of user's involved in this activity
        /// </summary>
        public int Count { get { return (int)(double)usersObject.GetProperty("Count"); } }
        /// <summary>
        /// This client's User
        /// </summary>
        public User Me { get { return new User((ScriptObject)usersObject.GetProperty("Me")); } }
        /// <summary>
        /// The other user
        /// </summary>
        public User RemoteUser 
        { 
            get 
            {
                foreach (var user in this)
                {
                    if (user != Me)
                        return user;
                }
                return null;
            } 
        }
        /// <summary>
        /// The first user to invite to this activity
        /// </summary>
        public User Inviter { get { return new User((ScriptObject)usersObject.GetProperty("Inviter")); } }


        public User this[int index]
        {
            get {
                return new User((ScriptObject)usersObject.Invoke("Item", index));
            }
        }

        #region IEnumerable<User> Members

        public IEnumerator<User> GetEnumerator()
        {
            return new UserEnumerator(this);
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new UserEnumerator(this);
        }

        #endregion

        class UserEnumerator : IEnumerator<User>
        {
            Users users;
            int index;

            public UserEnumerator(Users users)
            {
                this.users = users;
                index = -1;
            }

            #region IEnumerator<User> Members

            public User Current
            {
                get { return users[index]; }
            }

            #endregion

            #region IDisposable Members

            public void Dispose()
            {
            }

            #endregion

            #region IEnumerator Members

            object System.Collections.IEnumerator.Current
            {
                get { return index; }
            }

            public bool MoveNext()
            {
                index++;
                if (users.Count == index)
                    return false;
                return true;
            }

            public void Reset()
            {
                index = -1;
            }

            #endregion
        }

        /// <summary>
        /// Wrapper class for User ScriptObjects
        /// </summary>
        public class User
        {
            ScriptObject user;
            internal User(ScriptObject user)
            {
                this.user = user;
            }
            /// <summary>
            /// User's Live ID
            /// </summary>
            public string EMail { get { return (string)user.GetProperty("Email"); } }
            /// <summary>
            /// Get GlobalID, can be changed by firewalls and stuff. Trying to get this from remote users returns a blank string!
            /// </summary>
            public string GlobalIP { get { return (string)user.GetProperty("GlobalIP"); } }
            /// <summary>
            /// Get the user's local IP. Trying to get this from remote users returns a blank string!
            /// </summary>
            public string LocalIP { get { return (string)user.GetProperty("LocalIP"); } }
            /// <summary>
            /// User's Name
            /// </summary>
            public string Name { get { return (string)user.GetProperty("Name"); } }
            /// <summary>
            /// Passport User ID
            /// </summary>
            public ScriptObject PUID { get { return (ScriptObject)user.GetProperty("PUID"); } }

            public override bool Equals(object obj)
            {
                if (obj is User)
                    return false;

                return Equals((User)obj);
            }

            public bool Equals(User obj)
            {
                return EMail.Equals(obj.EMail);
            }


            public override int GetHashCode()
            {
                return EMail.GetHashCode();
            }

            public static bool operator ==(User a, User b)
            {
                // If both are null, or both are same instance, return true.
                if (System.Object.ReferenceEquals(a, b))
                {
                    return true;
                }

                // If one is null, but not both, return false.
                if (((object)a == null) || ((object)b == null))
                {
                    return false;
                }

                // Return true if the fields match:
                return a.Equals(b);
            }

            public static bool operator !=(User a, User b)
            {
                return !(a == b);
            }

        }

    }
}
